package com.bcx.crm.spring.controller;

import com.bcx.crm.java.java8.Artist;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created by ys on 2018/1/9
 */
@Controller
@RequestMapping("/urlRedirect")
public class URLRedirect {

    //此方法为简单的重定向，只能传递简单的值
    @RequestMapping(value = "/redirect",method = RequestMethod.GET)
    public String processRegistration(Artist artist, Model model){
        artist.setName("liSi");
        artist.setAddress("heNan");
        model.addAttribute("name",artist.getName());
        model.addAttribute("address",artist.getAddress());
        return "redirect:/urlRedirect/redirect/{name}";
        //name="liSi",address="heNan",则请求结果:/artist/liSi?address=heNan
    }

    //1.flash会携带数据直到下一次请求才会消失，数据例如：对象
    @RequestMapping(value = "/redirect/liSi",method = RequestMethod.GET)
    public String processRegistration(Artist artist, RedirectAttributes redirectAttributes){
        artist.setName("san");
        artist.setAddress("上海");
        redirectAttributes.addAttribute("name",artist.getName());
        redirectAttributes.addFlashAttribute("artist",artist);
        return "redirect:/urlRedirect/redirect/{name}";
    }

    //2.对flash传递的参数进行接收
    @RequestMapping(value = "/redirect/{name}",method = RequestMethod.GET)//${name}只为占位符,可匹配任意
    public String showArtistInfo(@PathVariable String name,Model model){
        if (!model.containsAttribute("artist")){
            model.addAttribute("artist",new Artist());
        }
        Artist artist=(Artist)model.asMap().get("artist");//获取传递的对象
        return "public/index";
    }
    //访问外网--项目之外(redirect:的后面不要加/)
    @RequestMapping("/visitOutNetwork")
    public String visitOutNetwork(){
        return "redirect:https://www.csdn.net/";
    }

}
